package com.tqjc.system.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tqjc.system.common.constant.ConfigConstant;
import com.tqjc.system.common.constant.GlobalConstant;
import com.tqjc.system.common.entity.BO.ConfigInfoBO;
import com.tqjc.system.common.enums.ConfigCacheEnum;
import com.tqjc.system.core.model.AjaxJson;
import com.tqjc.system.core.model.PageInfo;
import com.tqjc.system.core.util.ResponseUtil;
import com.tqjc.system.system.entity.SysConfigEntity;
import com.tqjc.system.system.service.SysConfigService;
import com.tqjc.system.system.wrapper.ConfigInfoWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 86195
 * @version 1.0
 * @description 配置信息管理控制器
 * @date 2023/8/18 9:27
 */
@RequestMapping("/sys/config")
@Controller
@Slf4j
public class SysConfigController extends SysCommonController{

    @Autowired
    private SysConfigService configService;


    @RequestMapping("/query")
    @ResponseBody
    public void index(ConfigInfoBO configInfoBO, PageInfo<SysConfigEntity> page, HttpServletResponse response) {
        log.info("配置信息查询首页接口开始");
        try {

            IPage<SysConfigEntity> list = configService.index(page.getPageEntity(),configInfoBO);
            ResponseUtil.write(response, new ConfigInfoWrapper(list).wrap());
        } catch (Exception e){
            log.error("配置查询页面出现异常",e);
        }
        log.info("配置信息查询首页接口结束");

    }



    @RequestMapping("/query/id")
    public String getDetail(String idKey, Model model){
        log.info("配置详情查询接口开始");
        try {
            SysConfigEntity sysConfigEntity = configService.getById(idKey);
            model.addAttribute("msgDetail",sysConfigEntity);
        } catch (Exception e){
            log.error("配置详情查询接口出现异常",e);

        }
        log.info("配置查询接口结束");
        return "sys/manage/config/configDetail.html";
    }

    @RequestMapping("/modify")
    @ResponseBody
    public AjaxJson modify(ConfigInfoBO configInfoBO){
        AjaxJson j = new AjaxJson();
        log.info("配置信息更新接口开始");
        try {
            SysConfigEntity sysConfigEntity = SysConfigEntity.builder().idKey(Long.valueOf(configInfoBO.getIdKey()))
                    .configValue(configInfoBO.getConfigValue())
                    .configStatus(configInfoBO.getConfigStatus()).build();
            configService.updateById(sysConfigEntity);
            ConfigCacheEnum configCacheEnum = ConfigCacheEnum.getConfigCacheEnum(configInfoBO.getConfigId(), configInfoBO.getConfigType());
            if(configCacheEnum != null){
                Object value;
                String configValue = configInfoBO.getConfigValue();
                if(ConfigConstant.CONFIG_STATUS_UNEFFECTIVE.equals(configInfoBO.getConfigStatus())){
                    value = false;
                } else {
                    value = Boolean.valueOf(configValue);
                }
                configCacheEnum.refreshCache(value);
            }

        } catch (Exception e){
            log.error("配置信息更新出现异常",e);
            j.setSuccess(false);
            j.setMsg("配置信息更新出现异常");
            return j;
        }
        j.setSuccess(true);
        j.setMsg("操作成功");
        log.info("配置信息更新接口结束");
        return j;
    }


    @RequestMapping("/getVisitFlag")
    @ResponseBody
    public AjaxJson getVisitFlag(String idKey, Model model){
        AjaxJson j = new AjaxJson();
        log.info("查询参观标识接口开始");

        try {

            j.setObj(GlobalConstant.VISIT_MODEL_FLAG);
        } catch (Exception e){
            j.setSuccess(false);
            log.error("查询参观标识接口出现异常",e);

        }
        log.info("查询参观标识接口结束");
        return j;
    }



    @RequestMapping("/index")
    public String toConfigIndex(){
        return "sys/manage/config/index.html";

    }



}
